using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.HtmlControls;

namespace MessageBoardControl
{
	/// <summary>
	/// Summary description for AddGroup.
	/// </summary>
	[DefaultProperty("Text"), 
		ToolboxData("<{0}:AddGroup runat=server></{0}:AddGroup>")]
	public class AddGroup : Control, INamingContainer 
	{
		#region Properties
		private string text;
	
		[Bindable(true), 
			Category("Appearance"), 
			DefaultValue("")] 
		public string Text 
		{
			get
			{
				return text;
			}

			set
			{
				text = value;
			}
		}

		private string urlAfter;
		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public string URLAfter 
		{
			get
			{
				return urlAfter;
			}

			set
			{
				urlAfter = value;
			}
		}

		private MessageBoard.User currentUser;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.User CurrentUser 
		{
			get
			{
				return currentUser;
			}

			set
			{
				currentUser = value;
			}
		}

		private MessageBoard.Language language;

		[Bindable(true), 
		Category("Appearance"), 
		DefaultValue("")] 
		public MessageBoard.Language Language 
		{
			get
			{
				return language;
			}

			set
			{
				language = value;
			}
		}
		#endregion

		#region Translation
		// define the control to be translated
		private MessageBoard.CustomControl customControl = MessageBoard.CustomControl.Get("AddGroup");

		/// <summary>
		/// Translate the words.
		/// </summary>
		/// <param name="word"></param>
		/// <returns></returns>
		private string Translate(string word)
		{
			return MessageBoard.Translation.Get(customControl, word, Language).Equal;
		}
		#endregion

		/// <summary> 
		/// Render this control to the output parameter specified.
		/// </summary>
		/// <param name="output"> The HTML writer to write out to </param>
		//protected override void Render(HtmlTextWriter output)
		//{
		//	output.Write(Text);
		//}

		protected override void CreateChildControls() 
		{
			// Check the required permissions
			if(MessageBoard.Role.Check(currentUser, MessageBoard.Role.Permission.CanAddGroups) !=  MessageBoard.Role.Allow.Yes)
			{
				return;
			}

			// The Group Items
            TextBox txtTitle = new TextBox();
			TextBox txtDescription = new TextBox();
			DropDownList cmbCategory = new DropDownList();
			Button cmdAdd = new Button();
			
			// Set the Title TextBox properties
			txtTitle.Text ="";
			txtTitle.MaxLength = 100;
			txtTitle.Width = 500;
			txtTitle.ID="txtTitle";

			// Set the Description TextBox properties
			txtDescription.Text="";
			txtDescription.TextMode = TextBoxMode.MultiLine;
			txtDescription.Height = 100;
			txtDescription.Width = 500;
			txtDescription.ID = "txtDescription";

			// Set the Category DropDownList properties
			ListItem itmYes = new ListItem(Translate("Yes"), "true");
			ListItem itmNo = new ListItem(Translate("No"), "false");
			cmbCategory.Items.Add(itmYes);
			cmbCategory.Items.Add(itmNo);
			cmbCategory.ID = "cmbCategory";

			// Set the Add button properties
			cmdAdd.Text = Translate("Add");

			// Put the contols in a table.
			HtmlTable table = new HtmlTable();
			table.Attributes.Add("width", "100%");
			HtmlTableRow tr0 = new HtmlTableRow();
			table.Controls.Add(tr0);
			HtmlTableCell td0 = new HtmlTableCell();
			tr0.Controls.Add(td0);
			td0.Attributes.Add("class", "Forum_Title1");
			td0.InnerHtml = Translate("Add new Group");

			HtmlTableRow tr01 = new HtmlTableRow();
			table.Controls.Add(tr01);
			HtmlTableCell td01 = new HtmlTableCell();
			tr01.Controls.Add(td01);
			td01.Attributes.Add("class", "Forum_Title4");

			// Table 2
			HtmlTable table2 = new HtmlTable();
			table2.Attributes.Add("width", "100%");

			// Title
			HtmlTableRow tr1 = new HtmlTableRow();
			table2.Controls.Add(tr1);
			HtmlTableCell td11 = new HtmlTableCell();
			HtmlTableCell td12 = new HtmlTableCell();
			td11.InnerHtml = Translate("Title")+":";
			td11.Attributes.Add("class", "Forum_Text");
			td12.Controls.Add(txtTitle);
			tr1.Controls.Add(td11);
			tr1.Controls.Add(td12);
			

			// Description
			HtmlTableRow tr2 = new HtmlTableRow();
			table2.Controls.Add(tr2);
			HtmlTableCell td21 = new HtmlTableCell();
			HtmlTableCell td22 = new HtmlTableCell();
			td21.InnerHtml = Translate("Description")+":";
			td21.Attributes.Add("class", "Forum_Text");
			td21.Attributes.Add("vAlign", "top");
			td22.Controls.Add(txtDescription);
			tr2.Controls.Add(td21);
			tr2.Controls.Add(td22);

			// WithCategories
			HtmlTableRow tr4 = new HtmlTableRow();
			table2.Controls.Add(tr4);
			HtmlTableCell td41 = new HtmlTableCell();
			HtmlTableCell td42 = new HtmlTableCell();
			td41.InnerHtml = Translate("Categories")+":";
			td41.Attributes.Add("class", "Forum_Text");
			td41.Attributes.Add("vAlign", "top");
			td42.Controls.Add(cmbCategory);
			tr4.Controls.Add(td41);
			tr4.Controls.Add(td42);


			// Button
			HtmlTableRow tr3 = new HtmlTableRow();
			table2.Controls.Add(tr3);
			HtmlTableCell td31 = new HtmlTableCell();
			HtmlTableCell td32 = new HtmlTableCell();
			td31.InnerHtml = "";
			td32.Controls.Add(cmdAdd);
			tr3.Controls.Add(td31);
			tr3.Controls.Add(td32);

			td01.Controls.Add(table2);

			// A table to make the border
			HtmlTable tableOut = new HtmlTable();
			HtmlTableRow trOut = new HtmlTableRow();
			HtmlTableCell tdOut = new HtmlTableCell();
			tableOut.BgColor = "#000000";
			tableOut.Attributes.Add("width", "100%");
			tableOut.CellPadding = 0;
			tableOut.CellSpacing = 0;
			table.CellSpacing = 1;
			tdOut.Controls.Add(table);
			trOut.Controls.Add(tdOut);
			tableOut.Controls.Add(trOut);

			// Add the table to the control
			Controls.Add(tableOut);

			// Add the button Event
			cmdAdd.Click += new EventHandler(cmdAdd_Click);
                  
		}

		/// <summary>
		/// Add the new Group.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void cmdAdd_Click(Object sender, EventArgs e)
		{
			TextBox txtTitle = ((TextBox)FindControl("txtTitle"));
			TextBox txtDescription = ((TextBox)FindControl("txtDescription"));
			DropDownList cmbCategory = ((DropDownList)FindControl("cmbCategory"));
			if(txtTitle.Text!="" || ((TextBox)Controls[1]).Text!="")
			{
				// Adding the group
				MessageBoard.Group group = new MessageBoard.Group();
				group.Title= txtTitle.Text;
				group.Description = txtDescription.Text;
				group.WithCategories = Convert.ToBoolean(cmbCategory.SelectedValue);
				group.AddedBy = currentUser;
				group.AddedOn = DateTime.Now;
				group.Insert();

				// Add a default category if this group will not display the categories
				if(group.WithCategories==false)
				{
					MessageBoard.Category category = new MessageBoard.Category();
					category.Title = group.Title;
					category.Description = group.Description;
					category.Group = group;
					category.Active = true;
					category.SortingOrder = 50;
					category.Insert();
				}

				txtTitle.Text="";
				txtDescription.Text="";
				this.EnsureChildControls();
				this.Page.Response.Redirect(urlAfter);
			}
		}

	}
}
